package cn.gcyl.module.biz.service.platforminformation;

import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.module.biz.controller.admin.platforminformation.vo.*;
import cn.gcyl.module.biz.dal.dataobject.SysMemberUserDO;
import cn.gcyl.module.biz.dal.dataobject.platforminformation.PlatformInformationDO;
import cn.gcyl.module.biz.dal.dataobject.useractionlog.UserActionLogDO;
import cn.gcyl.module.biz.dal.mysql.SysMemberUserMapper;
import cn.gcyl.module.biz.dal.mysql.platforminformation.PlatformInformationMapper;
import cn.gcyl.module.biz.dal.mysql.useractionlog.UserActionLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.PLATFORM_INFORMATION_NOT_EXISTS;

/**
 * 平台信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PlatformInformationServiceImpl implements PlatformInformationService {

    @Resource
    private PlatformInformationMapper platformInformationMapper;
    @Resource
    private UserActionLogMapper userActionLogMapper;
    @Resource
    private SysMemberUserMapper sysMemberUserMapper;

    @Override
    public Long createPlatformInformation(PlatformInformationSaveReqVO createReqVO) {
        // 插入
        PlatformInformationDO platformInformation = BeanUtils.toBean(createReqVO, PlatformInformationDO.class);
        if (createReqVO.getTitleType().equals("0")){
            platformInformation.setTitleType("项目采购商机");
        }else if (createReqVO.getTitleType().equals("1")){
            platformInformation.setTitleType("项目分包商机");
        }else if (createReqVO.getTitleType().equals("2")){
            platformInformation.setTitleType("集中招募商机");
        }
        platformInformationMapper.insert(platformInformation);

        // 返回
        return platformInformation.getId();
    }

    @Override
    public void updatePlatformInformation(PlatformInformationSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformInformationExists(updateReqVO.getId());
        // 更新
        PlatformInformationDO updateObj = BeanUtils.toBean(updateReqVO, PlatformInformationDO.class);
        platformInformationMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatformInformation(Long id) {
        // 校验存在
        validatePlatformInformationExists(id);
        // 删除
        platformInformationMapper.deleteById(id);
    }

    @Override
        public void deletePlatformInformationListByIds(List<Long> ids) {
        // 删除
        platformInformationMapper.deleteByIds(ids);
        }


    private void validatePlatformInformationExists(Long id) {
        if (platformInformationMapper.selectById(id) == null) {
            throw exception(PLATFORM_INFORMATION_NOT_EXISTS);
        }
    }

    @Override
    public PlatformInformationDO getPlatformInformation(Long id) {
        return platformInformationMapper.selectById(id);
    }

    @Override
    public PlatformInformationDO getAppPlatformInformation(Long id) {
        //访问一次接口浏览量加1
        PlatformInformationDO platformInformationDO = platformInformationMapper.selectById(id);
        platformInformationDO.setVisitors(platformInformationDO.getVisitors()+1);
        platformInformationMapper.updateById(platformInformationDO);
        //添加操作日志
        UserActionLogDO userActionLogDO = new UserActionLogDO();
        userActionLogDO.setUserId(getLoginUserId());
        userActionLogDO.setAction("访问平台信息:"+platformInformationDO.getPurchasingCompany());
        userActionLogMapper.insert(userActionLogDO);

        //非会员空户只能查看一次,添加查看记录
        Long loginUserId = getLoginUserId();
        SysMemberUserDO sysMemberUserDO = sysMemberUserMapper.selectById(loginUserId);
        sysMemberUserDO.setViewingTime(LocalDateTime.now());
        sysMemberUserMapper.updateById(sysMemberUserDO);
        return platformInformationDO;
    }

    @Override
    public PageResult<PlatformInformationDO> getPlatformInformationPage(PlatformInformationPageReqVO pageReqVO) {
        return platformInformationMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<PlatformInformationDO> getAppPlatformInformationPage(AppPlatformInformationPageReqVO pageReqVO) {
        return platformInformationMapper.getAppPlatformInformationPage(pageReqVO);
    }
    @Override
    public PageResult<PlatformInformationDO> AppPlatformInformationListReqVO(AppPlatformInformationListReqVO pageReqVO) {
        return platformInformationMapper.getAppPlatformInformationList(pageReqVO);
    }

}